﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using qltscd.Helper;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

namespace qltscd.Controllers
{
    public class LoaiTienController : BaseController
    {
        #region Properties Section
        private QLTBEntities db = new QLTBEntities();
        #endregion

        //
        // GET: /LoaiThietBi/

        #region Views Section
        public ViewResult Index()
        {
            LTScreenClient screen = new LTScreenClient();
            if (Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT] != null)
            {
                screen = (LTScreenClient)Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }

        public ActionResult ShowPdf()
        {
            ReportClass rpt = new ReportClass();
            rpt.FileName = Server.MapPath("~/") + "//Reports//DanhMucLoaiTien.rpt";
            rpt.Load();
            //rpt.SetDataSource(db.DanhMucLoaiTiens);
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(stream, "application/pdf");
        }



        public FileResult ShowExcel()
        {
            ReportClass rpt = new ReportClass();
            rpt.FileName = Server.MapPath("~/") + "//Reports//DanhMucLoaiTien.rpt";
            rpt.Load();
            //rpt.SetDataSource(db.DanhMucHangSanXuats);
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
            return File(stream, "application/ms-excel", "BaoCaoDanhMucLoaiTien.xls");
        }

        /*private void Search(DVQLScreenClient screen)
        {
            throw new NotImplementedException();
        }*/
        [HttpPost]
        public ActionResult Index(LTScreenClient screen)
        {
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index()
        {
            LTScreenClient screen = new LTScreenClient();
            if (Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT] != null)
            {
                screen = (LTScreenClient)Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiTien));
        }
        public ActionResult Create()
        {
            Models.BaseClient.DanhMucLoaiTien lt = new Models.BaseClient.DanhMucLoaiTien() { IdentityLoaiTien = -1 };
            return View(lt);
        }
        public ActionResult SuaLoaiTien()
        {
            string id = Request.QueryString["id"];
            if (id != null)
            {
                int iden = Convert.ToInt32(id);
                DanhMucLoaiTien lt = db.DanhMucLoaiTiens.Single(i => i.IdentityLoaiTien == iden);
                Models.BaseClient.DanhMucLoaiTien ltC = new Models.BaseClient.DanhMucLoaiTien();
                ObjectHelper.Copy(lt, ltC);
                return View(ltC);

            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult ThongTinLoaiTien(int id)
        {
            DanhMucLoaiTien lt = db.DanhMucLoaiTiens.Single(i => i.IdentityLoaiTien == id);
            return View(lt);
        }
        #endregion

        #region CRUD Section
        [HttpPost]
        public ActionResult SuaLoaiTien(Models.BaseClient.DanhMucLoaiTien ltC)
        {
            if (ModelState.IsValid)
            {
                //code de luu lai
                DanhMucLoaiTien lt = db.DanhMucLoaiTiens.Single(i => i.IdentityLoaiTien== ltC.IdentityLoaiTien);
                if (db.DanhMucLoaiTiens.Count(o => o.MaLoaiTien == ltC.MaLoaiTien) > 0)
                {
                    ViewData[SessionConstants.ERROR_MESSAGE] = "Mã loại tiền " + ltC.MaLoaiTien + " đã tồn tại";
                    return View(ltC);
                }
                ObjectHelper.Copy(ltC, lt);
                db.ObjectStateManager.ChangeObjectState(lt, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ltC);
        }

        [HttpPost]
        public ActionResult Create(Models.BaseClient.DanhMucLoaiTien danhmucloaitien)
        {
            if (ModelState.IsValid)
            {
                DanhMucLoaiTien lt = new DanhMucLoaiTien();
               
                ObjectHelper.Copy(danhmucloaitien, lt);
                db.DanhMucLoaiTiens.AddObject(lt);
                db.SaveChanges();
                return RedirectToAction("Create");
            }
            return View(danhmucloaitien);
        }
        public ActionResult XoaLoaiTien()
        {
            string id = Request.QueryString["id"];
            try
            {
                if (id != null)
                {
                    int iden = Convert.ToInt32(id);
                    DanhMucLoaiTien lt = db.DanhMucLoaiTiens.Single(i => i.IdentityLoaiTien == iden);
                    db.DanhMucLoaiTiens.DeleteObject(lt);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        #endregion

        #region Helper Section
        private void Search(LTScreenClient screen)
        {
            if (Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT] != null)
            {
                screen = (LTScreenClient)Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT];
            }
            else
            {
                screen = new LTScreenClient();
            }
            screen.DSLoaiTien.Clear();
            var list = db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien).ToList();
            foreach (DanhMucLoaiTien lt in list)
            {
                qltscd.Models.BaseClient.DanhMucLoaiTien ltClient = new Models.BaseClient.DanhMucLoaiTien();
                ObjectHelper.Copy(lt, ltClient);
                screen.DSLoaiTien.Add(ltClient);
            }
            Session[SessionScreenContansts.LOAI_TIEN_SCREENCLIENT] = screen;
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
    }
}